package com.tj.controller;

import com.tj.pojo.Doctor;
import com.tj.pojo.Users;
import com.tj.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    UsersService usersService;

    // ================================= 查询用户 =================================
    /*
     * -取得用户列表数据
     */
    @RequestMapping("/getUsersList")
    public Map<String, Object> getUsersList() throws Exception {
        List<Users> usersList = usersService.findAllList();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("usersList",usersList);
        return map;
    }
    /*
     * -按姓名进行模糊查找
     */
    @RequestMapping("/getUsersListByName")
    public Map<String, Object> getUsersListByName(@RequestParam String realName) throws Exception {
        List<Users> usersList = usersService.findUsersListByName(realName);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("usersList", usersList);
        return map;
    }
    /*
     * -按ID查
     */
    @RequestMapping("/getUsersListById")
    public Map<String, Object> getUsersListById(@RequestParam String userId) throws Exception {
        List<Users> usersList = usersService.findUsersListById(userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("usersList",usersList);
        return map;
    }
    // ================================= 增加用户 =================================
    @RequestMapping("/insertUsers")
    public int insertUsers(@RequestBody Users users) {
        return usersService.insertUsers(users);
    }
    // ================================= 修改用户 =================================
    @RequestMapping("/updateUsers")
    public int updateUsers(@RequestBody Users users) {
        return usersService.updateUsers(users);
    }
    // ================================= 删除用户 =================================
    /*
     * -执行用户删除的操作
     */
    @RequestMapping("/deleteUsers")
    public Map<String, Object> deleteUsers(HttpSession session, Users users) throws Exception {
        int num = usersService.deleteUsers(users);
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0) {
            map.put("status", "1");
        } else {
            map.put("status", "0");
        }
        return map;
    }
    @RequestMapping("users/getUsersByUserIdByPass")
    public Users getUsersByUserIdByPass(@RequestBody Users users) {
        return usersService.login(users);
    }

}
